package com.study.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;



//@Configuration
@EnableWebSecurity//指定为Spring Security配置类
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                //设置这3个路径放行，即不需要经过权限校验
                .antMatchers("/", "/welcome", "/login").permitAll()
                .anyRequest().authenticated()
                    .and()
                //设置登录的页面为自己的页面
                .formLogin().loginPage("/login").defaultSuccessUrl("/home")
                    .and()
                //增加注销的功能
                .logout().permitAll()
         ;
    }




    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())

                .withUser("admin")

                //就是官网的password
                .password("$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG")
                //.password("$2a$10$Zd8ywFPIbuWUrU4hcG/0A.4zf4dgzQdftZmhkxJYt4PNI1j0Nj2yC") 这个是密码123456加密后的值
                .roles("USER");
    }
}
